var util = require("../../../utils/util.js"),
  api = require("../../../utils/api.js"),
  app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    credential_aspect: '', 
    mobile: '',
    credential_front: '',
    diploma_img: '',
    social_security_img: '',
    is_single_img: '',
    baseImgUrl: app.globalData.baseImgUrl,
    bangsHeight:wx.getStorageSync('navBarObj').bangsHeight,
    baseImgUrl1: app.globalData.pathUrl+'/public/'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getUserInfo()
  },
  async getUserInfo() {
    let { data } = await api.default.getUserInfo();
    if (data) {
      this.setData({
        credential_aspect: data.credential_aspect,
        mobile: data.mobile,
        credential_front: data.credential_front,
        diploma_img: data.diploma_img,
        social_security_img: data.social_security_img,
        is_single_img: data.is_single_img
      })
    }
  },
  whyUnserName() {
    wx.navigateTo({
      url: '/pages/mine/attestation/explain/explain',
    })
  },
  addImage(e) {
    const { type } = e.currentTarget.dataset;
    var self = this;
    wx.chooseMedia({
      count: 1,
      mediaType: ['image'],
      sourceType: ['album', 'camera'],
      maxDuration: 30,
      camera: 'back',
      success(res) {
        console.log('res', res)
        var tempFilePaths = res.tempFiles;
        self.setData({ uploadArr: [] })
        if (tempFilePaths.length > 0) {
          wx.showLoading({
            title: '上传中',
            mask: true
          })
          self.uploadImgs(tempFilePaths[0].tempFilePath, type);
        }
      }
    })
  },
  showImage:function(event){
    const {src,list}=event.currentTarget.dataset;
    wx.previewImage({
     current: src,
     urls: Array.isArray(list)?list.map(o=>this.data.baseImgUrl+o):[src]
    })
  },
  uploadImgs(filePath, type) {
    wx.uploadFile({
      url: app.globalData.baseUrl + 'common/index/uploadImg',//仅为示例，非真实的接口地址//接口连接
      filePath: filePath,
      name: 'file',
      success: (res) => {
        if (res.data) {
          let json = JSON.parse(res.data)
          if (json.code == 1) {
            let setDatas = {};
            setDatas[type] = json.data; 
            console.log('setDatas', setDatas);
            this.setData(setDatas)
            wx.showToast({ title: '上传成功', icon: 'success' })
          } else {
            wx.showToast({ title: '上传失败', icon: 'none' })
          }
        } else {
          wx.showToast({ title: res.msg, icon: 'none' })
        }
      }
    })
  },
  getPhoneNum: function (e) {
    console.log(e);
    if (e.detail.errMsg == "getPhoneNumber:ok") {
      var a = {
        sessionKey: wx.getStorageSync('session_key'),
        iv: e.detail.iv,
        encryptedData: e.detail.encryptedData
      };
      this.parsePhoneNumber(a);
    } else {
      wx.showModal({
        title: '提示',
        content: '您的手机号码获取出现了错误',
        cancelText: '知道了',
        showCancel: false
      })
    }
  },
  parsePhoneNumber: function (e) {
    api.default.getWxTel(e).then(res => {
      console.log(e);
      if (res.data) {
        let josn = JSON.parse(res.data);
        this.setData({ mobile: josn.phoneNumber })
      }
    })
  },
  getMobile(e) {
    this.setData({ mobile: e.detail })
  },
  getCredential(e) {
    this.setData({ credential_id: e.detail })
  },
  getNickname(e) {
    this.setData({ nickname: e.detail })
  },
  gotoNext(e) {
    const { type } = e.currentTarget.dataset;
    const { mobile, credential_aspect, credential_front, diploma_img, social_security_img, is_single_img } = this.data;
    if (mobile && !util.regularConfig.mobile(mobile)) return wx.showToast({ title: '电话格式错误', icon: 'none' })
    // if (!util.regularConfig.IDcard(credential_id)) return wx.showToast({ title: '身份证格式错误', icon: 'none' })
    // if (!util.regularConfig.isString(diploma_img)) return wx.showToast({ title: '请上传毕业证书图片', icon: 'none' })
    // if (!util.regularConfig.isString(social_security_img)) return wx.showToast({ title: '请上传社保证明', icon: 'none' })
    // if (!util.regularConfig.isString(is_single_img)) return wx.showToast({ title: '请上传单身已婚证明', icon: 'none' })
    const parsm = {
      mobile,
      credential_aspect,
      credential_front,
      diploma_img,
      social_security_img,
      is_single_img,
      type
    }
    // status,
      
    api.default.setMemberName(parsm).then(res => {
      if (res.code == 1) {
        wx.showModal({
          title: '提示',
          content: type == 1 ? '您的信息已保存草稿' : '您的实名认证已提交，请等待管理员审核',
          complete: (res) => {
            if (res.confirm) {
              if (type == 2) {
                wx.switchTab({
                  url: '/pages/home/home',
                })
              }
            }
          }
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})